package com.hedl.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.hedl.entity.User;
import com.hedl.mapper.UserMapper;
import com.hedl.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

//    @Autowired
//    private RedisTemplate<String, String>  redisTemplate;

    private String userKey = "user:";
    
    @Override
    public List<User> findAll() {
        return userMapper.findAll();
    }

    @Override
    public User selectById(Long id) {
        // 从缓存中获取数据
//        String s = redisTemplate.opsForValue().get(userKey + id);
//        if (StringUtils.isEmpty(s)){
            User user = userMapper.selectById(id);
//            if (user != null){
                // 存入缓存
//                redisTemplate.opsForValue().set(userKey + id, JSONObject.toJSONString(user), 2, TimeUnit.HOURS);
//                return user;
//            }
            // 为空存入空值，防止缓存被击穿
//            redisTemplate.opsForValue().set(userKey + id, JSONObject.toJSONString(user), 2, TimeUnit.MINUTES);
//        }
//        return JSONObject.parseObject(s, User.class);
        return user;
    }
}
